/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Golf_Manager;


import Golf_Manager.editor.doc.IntegerDocument;
import Golf_Manager.model.GroupMemberModel;
import Golf_Manager.print.PrintPreview;
import Golf_Manager.renderer.ColorColumnRenderer;
import Golf_Manager.sqlite.GroupDAO;
import Golf_Manager.vo.Group;
import Golf_Manager.vo.GroupDesc;
import Golf_Manager.vo.JoinMember;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import static java.awt.image.ImageObserver.WIDTH;
import java.awt.print.PrinterJob;
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.Sides;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author Administrator
 */
public class GroupSearchFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form GroupSearchFrame
     */
    public GroupSearchFrame() {
        initComponents();
        
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(JTextField.CENTER);
        //groupMemberTable설정 시작
        groupMemberTable.getColumnModel().getColumn(0).setPreferredWidth(50);
        groupMemberTable.getColumnModel().getColumn(0).setCellRenderer(renderer);
        groupMemberTable.getColumnModel().getColumn(1).setPreferredWidth(80);
        groupMemberTable.getColumnModel().getColumn(1).setCellRenderer(renderer);
        groupMemberTable.getColumnModel().getColumn(2).setPreferredWidth(70);
        groupMemberTable.getColumnModel().getColumn(2).setCellRenderer(renderer);
        
        JTextField jfScore = new JTextField();
        jfScore.setDocument(new IntegerDocument (3));
        jfScore.addFocusListener(new FocusAdapter() {
            
            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e); //To change body of generated methods, choose Tools | Templates.
                //System.out.println(e);
                groupMemberTable.getColumnModel().getColumn(3).getCellEditor().stopCellEditing();
            }
        });
        
        //jfScore.setHorizontalAlignment(JTextField.CENTER);
        
        groupMemberTable.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(jfScore));
        groupMemberTable.getColumnModel().getColumn(3).setCellRenderer(renderer);
        groupMemberTable.getColumnModel().getColumn(3).setPreferredWidth(100);
        
        //groupMemberTable설정 종료
        
        //groupTable설정 시작
        JTextField jfGroupField = new JTextField();
        jfGroupField.setHorizontalAlignment(JTextField.CENTER);
        ColorColumnRenderer colorColumnRenderer = new ColorColumnRenderer("groupTable");
        colorColumnRenderer.setHorizontalAlignment(JTextField.CENTER);
        //CELL 0설정 시작
        groupTable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(jfGroupField));
        renderer.setHorizontalAlignment(JTextField.CENTER);
        groupTable.getColumnModel().getColumn(0).setCellRenderer(renderer);
        groupTable.getColumnModel().getColumn(0).setPreferredWidth(50);
        //CELL 0설정 끝
        //CELL 1설정 시작
        groupTable.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(jfGroupField));
        groupTable.getColumnModel().getColumn(1).setCellRenderer(renderer);
        //CELL 1설정 끝
        //CELL 2설정 시작
        groupTable.getColumnModel().getColumn(2).setCellRenderer(colorColumnRenderer);
        //CELL 2설정 끝
        //CELL 3설정 시작
        //CELL 3설정 끝
        groupTable.getColumnModel().getColumn(3).setCellRenderer(colorColumnRenderer);
        //CELL 4설정 시작
        groupTable.getColumnModel().getColumn(4).setCellRenderer(colorColumnRenderer);
        //CELL 4설정 끝
        //CELL 더블클릭 설정
        groupTable.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    JTable target = (JTable)e.getSource();
                    int row = target.getSelectedRow();
                    int column = target.getSelectedColumn();
                    // do some action
                    //System.out.println("row :" + row + ", column :" + column);
                    Golf_Manager.model.GroupModel groupModel =  (Golf_Manager.model.GroupModel)groupTable.getModel();
                    Group group = groupModel.getGroup(row);
                    if(column > 0 && column <= group.getGroupMemberCnt()){
                        JFrame dFrm = new JFrame();
                        MemberListDialog memberListDialog = new MemberListDialog(dFrm, "회원목록", true);
                        memberListDialog.setLocationRelativeTo(dFrm);
                        memberListDialog.setVisible(true);
                        Golf_Manager.vo.Member member = memberListDialog.getMember();
                        if(member != null){
                            setGroupMemberModify(member, row, column);
                        }
                    }
                }
            }
        });
        //CELL 더블클릭 종료
        //CELL 0 edit false 설정
        Golf_Manager.model.GroupModel groupModel =  (Golf_Manager.model.GroupModel)groupTable.getModel();
        groupModel.setCanEdit(0, false);
        //groupTable설정 설정 종료
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jDateGroupDate = new com.toedter.calendar.JDateChooser();
        jSeparator2 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        groupMemberTable = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jBtnGroupSearch = new javax.swing.JButton();
        jBtnGroupSave = new javax.swing.JButton();
        jBtnPrint = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        groupTable = new javax.swing.JTable();
        jBtnGroupSearchPop = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldgDate = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldgKey = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldgOrder = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldgRecYn = new javax.swing.JTextField();
        jButtonDelJoinMembe = new javax.swing.JButton();

        setTitle("편성조 조회 및 점수입력");
        setPreferredSize(new java.awt.Dimension(1024, 768));

        jLabel4.setText("조편성일자");

        jDateGroupDate.setDateFormatString("yyyy-MM-dd");

        groupMemberTable.setModel(new  Golf_Manager.model.GroupMemberModel());
        groupMemberTable.getTableHeader().setReorderingAllowed(false);
        groupMemberTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(groupMemberTable);

        jLabel2.setText("조편성 성적입력");

        jLabel3.setText("편성조");

        jBtnGroupSearch.setText("조회");
        jBtnGroupSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnGroupSearchActionPerformed(evt);
            }
        });

        jBtnGroupSave.setText("저장");
        jBtnGroupSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnGroupSaveActionPerformed(evt);
            }
        });

        jBtnPrint.setText("출력");
        jBtnPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnPrintActionPerformed(evt);
            }
        });

        groupTable.setModel(new Golf_Manager.model.GroupModel());
        groupTable.setCellSelectionEnabled(true);
        groupTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        groupTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(groupTable);

        jBtnGroupSearchPop.setText("조편성목록");
        jBtnGroupSearchPop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnGroupSearchPopActionPerformed(evt);
            }
        });

        jLabel1.setText("조편성일자 : ");

        jTextFieldgDate.setEditable(false);

        jLabel5.setText("조편성 기준 : ");

        jTextFieldgKey.setEditable(false);

        jLabel6.setText("정렬기준 : ");

        jTextFieldgOrder.setEditable(false);

        jLabel7.setText("최근전적반영여부 :");

        jTextFieldgRecYn.setEditable(false);

        jButtonDelJoinMembe.setText("조원삭제");
        jButtonDelJoinMembe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDelJoinMembeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator3)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDateGroupDate, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBtnGroupSearchPop)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBtnGroupSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(619, 619, 619))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(192, 192, 192)
                                .addComponent(jBtnGroupSave, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonDelJoinMembe)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jBtnPrint))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane2)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldgDate, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldgKey, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldgOrder, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldgRecYn, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jSeparator2)))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jBtnGroupSave, jBtnPrint, jButtonDelJoinMembe});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel7, jTextFieldgDate, jTextFieldgKey, jTextFieldgRecYn});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jDateGroupDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtnGroupSearch)
                    .addComponent(jBtnGroupSearchPop))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextFieldgRecYn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextFieldgOrder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextFieldgKey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextFieldgDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jBtnGroupSave)
                    .addComponent(jBtnPrint)
                    .addComponent(jButtonDelJoinMembe))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jBtnPrint, jButtonDelJoinMembe});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnGroupSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnGroupSearchActionPerformed
        // TODO add your handling code here:
        if(jDateGroupDate.getDate() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            String gDate = sdf.format(jDateGroupDate.getDate());
            //System.out.println(sdf.format(jDateGroupDate.getDate()));
            groupSearch(gDate);
        } else {
            JOptionPane.showMessageDialog(rootPane, "조편성일자를 입력 하십시오.", "확인", WIDTH);   
        }    
    }//GEN-LAST:event_jBtnGroupSearchActionPerformed
    
    private void groupSearch(String gDate){
        //GROPT_MST 조회
        GroupDesc groupDesc =  GroupDAO.selectGroupMst(gDate);
        if(groupDesc != null){
              //System.out.println(groupDesc.toString());      
              jTextFieldgDate.setText(groupDesc.getgDate());
              jTextFieldgKey.setText(groupDesc.getgKey());
              jTextFieldgOrder.setText(groupDesc.getgOrder());
              jTextFieldgRecYn.setText(groupDesc.getgRecYn() == "Y" ? "반영" : "반영하지않음");
        } else {
            jTextFieldgDate.setText("");
            jTextFieldgKey.setText("");
            jTextFieldgOrder.setText("");
            jTextFieldgRecYn.setText("");
        }
        //JOINMEMBERS 조회
        List<JoinMember> JoinMembers = GroupDAO.selectGroupJoinMembers(gDate);
        //GROPT_DTL 조회
        List<Group> groups = GroupDAO.selectGroupDtl(gDate);

        GroupMemberModel groupMemberModel = (GroupMemberModel)groupMemberTable.getModel();
        Golf_Manager.model.GroupModel groupModel =  (Golf_Manager.model.GroupModel)groupTable.getModel();
        //JOINMEMBERS 적용

        groupMemberModel.setJoinMembers(JoinMembers);
        groupMemberTable.repaint();


        //GROPT_DTL에 JOINMEMBERS 설정 후 적용
        if(JoinMembers.size() > 0 && groups.size() > 0){

            for(Group group : groups){
                for(JoinMember joinMember : JoinMembers){
                   if(group.getGroupRow() == joinMember.getGroupRow()){
                       switch(joinMember.getGroupCol()){
                           case 1: group.setMemberSeq1(joinMember.getMemberSeq()); group.setMemberDesc1(joinMember.getName()/* + "(" + joinMember.getBirth() + ")"*/); break;
                           case 2: group.setMemberSeq2(joinMember.getMemberSeq()); group.setMemberDesc2(joinMember.getName()/* + "(" + joinMember.getBirth() + ")"*/); break;
                           case 3: group.setMemberSeq3(joinMember.getMemberSeq()); group.setMemberDesc3(joinMember.getName()/* + "(" + joinMember.getBirth() + ")"*/); break;
                           case 4: group.setMemberSeq4(joinMember.getMemberSeq()); group.setMemberDesc4(joinMember.getName()/* + "(" + joinMember.getBirth() + ")"*/); break;
                           default: break;
                       }
                    }
                }
            }
        }

        groupModel.setGroups(groups);
        groupTable.repaint();

        if(groupDesc != null && JoinMembers.size() > 0 && groups.size() > 0){
            JOptionPane.showMessageDialog(rootPane, "조회 되었습니다.", "확인", WIDTH);
        } else {
            JOptionPane.showMessageDialog(rootPane, "해당 조편성일자로 저장된 조편성이 없습니다.", "확인", WIDTH);
        }
    }
    
    private void jBtnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnPrintActionPerformed
        // TODO add your handling code here:
        //JOptionPane.showMessageDialog(rootPane, "출력", "확인", WIDTH);
        String printDate = jTextFieldgDate.getText().substring(0, 4)+"년" + jTextFieldgDate.getText().substring(5, 7)+"월" + jTextFieldgDate.getText().substring(8, 10)+"일";
        PrinterJob printerJob = PrinterJob.getPrinterJob();  
        HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();  

        attributes.add(new MediaPrintableArea(6, 6, 198, 278, MediaPrintableArea.MM)); // A4: 210x297mm  
        attributes.add(Sides.DUPLEX);  
        attributes.add(OrientationRequested.LANDSCAPE);  
//        
//        
        new PrintPreview(
            groupTable.getPrintable(JTable.PrintMode.FIT_WIDTH, new MessageFormat(printDate + " 조편성"), new MessageFormat("Page {0}") ),   
            printerJob.getPageFormat(attributes)
        );  
    }//GEN-LAST:event_jBtnPrintActionPerformed

    private void jBtnGroupSearchPopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnGroupSearchPopActionPerformed
        // TODO add your handling code here:
        JFrame dFrm = new JFrame();
        GroupListDialog groupListDialog = new GroupListDialog(dFrm, "조편성목록", true);
        groupListDialog.setLocationRelativeTo(dFrm);
        groupListDialog.setVisible(true);
        String gDate = groupListDialog.getReturnGdate();
        try {        
            if(gDate != null && !gDate.equals("")){
                SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd");     
                jDateGroupDate.setDate(transFormat.parse(gDate));
                groupSearch(gDate);
            }
        } catch (ParseException ex) {
            Logger.getLogger(GroupSearchFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBtnGroupSearchPopActionPerformed

    private void jBtnGroupSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnGroupSaveActionPerformed
        // TODO add your handling code here:
        GroupMemberModel groupMemberModel = (GroupMemberModel)groupMemberTable.getModel();
        if(groupMemberModel.getJoinMembers().size() > 0){
            groupMemberTable.editCellAt(0,0); //스코어입력후 바로 저장시 데이터가 적용 안됨.. ㅋ
            if(JOptionPane.showConfirmDialog(rootPane, "저장하시겠습니까??", "확인", JOptionPane.YES_NO_OPTION) == 0){
//                System.out.println(jTextFieldgDate.getText());
//                for(JoinMember joinMember : groupMemberModel.getJoinMembers()){
//                    System.out.println(joinMember.toString());
//                }
                if(GroupDAO.saveJoinMembers(jTextFieldgDate.getText(), groupMemberModel.getJoinMembers())){
                   JOptionPane.showMessageDialog(rootPane, "저장되었습니다.", "확인", WIDTH); 
                } else {
                    JOptionPane.showMessageDialog(rootPane, "저장중 오류가 발생하였습니다.", "확인", WIDTH);
                }
            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "편성된 조원이 없습니다. 조편성을 하시고 저장 하십시오.", "확인", WIDTH);
        }
    }//GEN-LAST:event_jBtnGroupSaveActionPerformed

    private void jButtonDelJoinMembeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDelJoinMembeActionPerformed
        // TODO add your handling code here:
        GroupMemberModel groupMemberModel = (GroupMemberModel)groupMemberTable.getModel();
        Golf_Manager.model.GroupModel groupModel =  (Golf_Manager.model.GroupModel)groupTable.getModel();
        int selGroupRow =  groupTable.getSelectedRow();
        int selGroupCol = groupTable.getSelectedColumn();
        
        if(selGroupRow < 0 || selGroupCol < 1) {
            JOptionPane.showMessageDialog(rootPane, "삭제 하고자 하는 조를 선택하십시오.", "확인", WIDTH);
        } else {
            Group group = groupModel.getGroup(selGroupRow);
                
            String delJoinMemberSeq = "";

            switch(selGroupCol){
                case 1: delJoinMemberSeq = group.getMemberSeq1(); break;
                case 2: delJoinMemberSeq = group.getMemberSeq2(); break;
                case 3: delJoinMemberSeq = group.getMemberSeq3(); break;
                case 4: delJoinMemberSeq = group.getMemberSeq4(); break;
            }
            if(delJoinMemberSeq != null && !delJoinMemberSeq.equals("")){
                for(int joinMemberRowIdx = 0; joinMemberRowIdx < groupMemberModel.getJoinMembers().size(); joinMemberRowIdx++){
                    if(groupMemberModel.getJoinMembers().get(joinMemberRowIdx).getMemberSeq().equals(delJoinMemberSeq)){
                            groupMemberModel.delJoinMember(joinMemberRowIdx);
                    }
                }
                groupModel.setGroupMember(selGroupRow, selGroupCol, -1, "", "");
                groupMemberTable.repaint();
                groupTable.repaint();
            } else {
                JOptionPane.showMessageDialog(rootPane, "삭제 하고자 하는 조원이 없습니다.", "확인", WIDTH);
            }   
        }
    }//GEN-LAST:event_jButtonDelJoinMembeActionPerformed

    private void setGroupMemberModify(Golf_Manager.vo.Member member, int selGroupRow, int selGroupCol){
        
        GroupMemberModel groupMemberModel = (GroupMemberModel)groupMemberTable.getModel();
        Golf_Manager.model.GroupModel groupModel =  (Golf_Manager.model.GroupModel)groupTable.getModel();
        
        //현재 선택한 컬럼에 맵버가 존가한다면 삭제
        String oldJoinMemberSeq = "";
        Group oldgroup = groupModel.getGroup(selGroupRow);
        switch(selGroupCol){
            case 1: oldJoinMemberSeq = oldgroup.getMemberSeq1(); break;
            case 2: oldJoinMemberSeq = oldgroup.getMemberSeq2(); break;
            case 3: oldJoinMemberSeq = oldgroup.getMemberSeq3(); break;
            case 4: oldJoinMemberSeq = oldgroup.getMemberSeq4(); break;    
        }
        if(oldJoinMemberSeq != null && !oldJoinMemberSeq.equals("")){
            for(int oldJoinMemberRowIdx = 0; oldJoinMemberRowIdx < groupMemberModel.getJoinMembers().size(); oldJoinMemberRowIdx++){
                if(groupMemberModel.getJoinMembers().get(oldJoinMemberRowIdx).getMemberSeq().equals(oldJoinMemberSeq)){
                        groupMemberModel.delJoinMember(oldJoinMemberRowIdx);
                }
            }
            groupModel.setGroupMember(selGroupRow, selGroupCol, -1, "", "");
        }
        //추가되는 맵버가 기존에 있는 경우 삭제
        int oldGroupRow = -1;
        int oldGroupCol = 0;
        for(int joinMemberRowIdx = groupMemberModel.getJoinMembers().size()-1; joinMemberRowIdx >= 0; joinMemberRowIdx--){
            if(groupMemberModel.getJoinMembers().get(joinMemberRowIdx).getMemberSeq().equals(member.getMemberSeq())){
                oldGroupRow = groupMemberModel.getJoinMembers().get(joinMemberRowIdx).getGroupRow();
                oldGroupCol = groupMemberModel.getJoinMembers().get(joinMemberRowIdx).getGroupCol();
                groupMemberModel.delJoinMember(joinMemberRowIdx);
                break;
            }
        }
        if(oldGroupRow > -1 && oldGroupCol > 0){
            groupModel.setGroupMember(oldGroupRow, oldGroupCol, -1, "", "");
        }
        //현재 데이터 설정
        groupMemberModel.addJoinMember(new JoinMember(groupModel.getGroup(selGroupRow).getGroupName(), selGroupRow, selGroupCol, member.getMemberSeq(), member.getName(), member.getBirth()));
        //JoinMembers sort
        Collections.sort(groupMemberModel.getJoinMembers());
        
        groupModel.setGroupMember(selGroupRow, selGroupCol, -1, member.getMemberSeq(), member.getName() /*+ "(" + member.getBirth() + ")"*/);
        //groupMemberModel.setJoinMembers(JoinMembers);
        groupMemberTable.repaint();
        groupTable.repaint();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable groupMemberTable;
    private javax.swing.JTable groupTable;
    private javax.swing.JButton jBtnGroupSave;
    private javax.swing.JButton jBtnGroupSearch;
    private javax.swing.JButton jBtnGroupSearchPop;
    private javax.swing.JButton jBtnPrint;
    private javax.swing.JButton jButtonDelJoinMembe;
    private com.toedter.calendar.JDateChooser jDateGroupDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTextField jTextFieldgDate;
    private javax.swing.JTextField jTextFieldgKey;
    private javax.swing.JTextField jTextFieldgOrder;
    private javax.swing.JTextField jTextFieldgRecYn;
    // End of variables declaration//GEN-END:variables
}
